package org.jeecg.modules.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.biz.entity.BurnIncome;
import org.jeecg.modules.biz.entity.EventWithdraw;
import org.jeecg.modules.biz.mapper.EventWithdrawMapper;
import org.jeecg.modules.biz.service.BurnIncomeService;
import org.jeecg.modules.biz.service.EventWithdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class EventWithdrawServiceImpl extends ServiceImpl<EventWithdrawMapper, EventWithdraw> implements EventWithdrawService {
    @Autowired
    private EventWithdrawMapper withdrawMapper;
    @Autowired
    private BurnIncomeService incomeService;

    @Override
    public EventWithdraw getLatestOne() {
        return withdrawMapper.getLatestOne();
    }

    @Override
    @Transactional
    public boolean saveAndUpdateBurn(EventWithdraw withdraw) {
        // 保存燃烧提现事件明细
        this.save(withdraw);

        // 更新用户燃烧收益表 总收益及冻结额度
        BurnIncome burnIncome = incomeService.getOne(new QueryWrapper<BurnIncome>().eq("address", withdraw.getAddress()));
        burnIncome.setBalance(burnIncome.getBalance() - withdraw.getAmount());
        burnIncome.setFreezingAmount(burnIncome.getFreezingAmount() - withdraw.getAmount());
        incomeService.updateById(burnIncome);

        return true;
    }
}
